<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
//include_once $site_path."/header.php";

$username="janes";
$password= "janes";
$username="ima";
$password= "ima2006";
$assetID=0;
if(isset($_REQUEST['assetID'])&&$_REQUEST['assetID']!="")
{
  $assetID=$_REQUEST['assetID'];
}
//insert

//get image 
$link='http://huarenads.com/images/listings/3_thumb.jpg';
$link='http://www.mathinstitutes.org/images/protein_thumb.jpg';
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_POST, 0);
 
curl_setopt($ch,CURLOPT_URL,$link);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$image=curl_exec($ch);
 
curl_close($ch);
//echo $image."<br>";
//image =file_get_contents('http://huarenads.com/images/listings/3_thumb.jpg');
//$image = base64_encode($image);
$image = strtr(base64_encode(addslashes(gzcompress(serialize($image),9))), '+/=', '-_,');
/*$image = json_encode($image);
$image = json_decode($image);
$thumbnail_image_file = "2/83/3_thumb.jpg";
	$savefile = fopen($_SERVER['DOCUMENT_ROOT']."/math-videos/images/$thumbnail_image_file", 'wb');			
	fwrite($savefile, $image);
			fclose($savefile);
			//echo "<img src='".$site_http."images/2/83/3_thumb.jpg' >";	
	$content=	file_get_contents($site_http."images/2/83/3_thumb.jpg");
echo '<img src="data:image/jpg;base64,'.$content.'" />';*/
//echo $image
/**************/
$lectureDate = strtotime(date('2013-06-08 14:00:00'));
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="insert")
{
  $data =  array(
			  	"username" =>$username,
				"password" =>$password,
				"title" => "Dooes Neuroscience Need Mathematics testing", 
			  "lectureDate"=> "$lectureDate",
			  "remoteURL" => "http://www.ima.umn.edu",
			  "location" => "Mathematics Bioscience Institute",
				"eventTitle" => "Testing", 
			  "presenters" => array( "familyName" => "Terman" ,"givenName" => "Test",
				"title" => "Professor",
				"company" => "test company"),
				"keywords" => array("Math","test", "Maths"),
				"mscNumbers" => array("3456","9808"),
				"timecode" => array(array("timecode"=>35,
				"timecode_type" => "Slide",
				"data" =>"Modeling in Ecology"),array("timecode"=>15,
				"timecode_type" => "Slide2",
				"data" =>"Modeling in Ecology2")),
				"rating" =>2
			  );

  $data_string = "json=".json_encode($data); 
 // echo "<div><b>How to call the api</b>:<br>".$site_http."api/save_asset.php?".$data_string."</div><br><b>Response</b>:<br>"; 
  $ch = curl_init($site_http.'api/save_asset.php');
} 
//upldate
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="update")
{
  $data =  array(
					"username" =>$username,
				"password" =>$password,
				"title" => "Dooes Neuroscience Need Mathematics testing", 
			  "lectureDate"=> "$lectureDate",
				"duration"=> "600",
			  "remoteURL" => "http://huarenads.com/",
			  "location" => "Mathematics Bioscience Institute",
				"eventTitle" => "Testing", 
			  "presenters" => array( "familyName" => "Cui" ,"givenName" => "Qing",
				"title" => "Professor",
				"company" => "test company"),
				"keywords" => array("Maths","testing"),
				"mscNumbers" => array("3333","9999"),
				"timecode" => array(array("timecode"=>100,
				"timecode_type" => "Slide",
				"data" =>"Modeling in Ecology"),array("timecode"=>15,
				"timecode_type" => "Slide2",
				"data" =>"Modeling in Ecology2")),
				"rating" =>2,
				"thumbnail" => array("image" => $image, "type"=>"jpg"),
				"assetID" =>"11"
			  );                                                                         
  $data_string = "json=".json_encode($data); 
	echo "<div><b>How to call the api</b>:<br>".$site_http."api/update_asset.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/update_asset.php');   
}
//get data
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="get" && $assetID !=0)
{
  $data =  array(
				"username" =>$username,
				"password" =>$passwords,
				"assetID" =>$assetID
			  );                                                                         
  $data_string = "json=".json_encode($data);
  echo "<div><b>How to call the api</b>:<br>http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/get_asset.php');    
  /******************************************/                                                               
}
//get data
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="get" && $assetID==0)
{
  $data =  array(
					"username" =>$username,
				"password" =>$password
			  );                                                                         
  $data_string = "json=".json_encode($data);
  echo "<div><b>How to call the api</b>:<br>".$site_http."api/get_asset.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/get_asset.php');    
  /******************************************/                                                               
}


//delete
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="delete")
{
  $data =  array(
				"username" =>$username,
				"password" =>$password,
				"assetID" =>$assetID
			  );                                                                         
  $data_string = "json=".json_encode($data);
  echo "<div><b>How to call the api</b>:<br>".$site_http."api/delete_asset.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/delete_asset.php');    
  /******************************************/                                                               
}
//update
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="timecode_edit")
{
  $data =  array(
				"username" =>$username,
				"password" =>$password,
				
				"timecode" => array(array("timecode"=>200,
				"timecode_type" => "Slide",
				"data" =>"Modeling in Ecology"),array("timecode"=>305,
				"timecode_type" => "Slide2",
				"data" =>"Modeling in Ecology2")),
				"assetID" =>"11"
			  );                                                                         
  $data_string = "json=".json_encode($data); 
	echo "<div><b>How to call the api</b>:<br>".$site_http."api/timecode_edit.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/timecode_edit.php');   
}
/*************************/
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="statistics_edit")
{
  $data =  array(
				"username" =>$username,
				"password" =>$password,
				"rating" =>2,
				"views" =>234,
				"assetID" =>"11"
			  );                                                                         
  $data_string = "json=".json_encode($data); 
	echo "<div><b>How to call the api</b>:<br>".$site_http."api/statistics_edit.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/statistics_edit.php');   
}
/*************************/
/******************************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']=="thumbnail_edit")
{
	if($_REQUEST['assetID'] !='')
	{
		$assetID = $_REQUEST['assetID'];
	}
  $data =  array(
				"username" =>$username,
				"password" =>$password,
				"thumbnail" => array("image" => $image,"type"=>"jpg"),
				"assetID" =>	$assetID
			  );   
				
  $data_string = "json=".json_encode($data); 
	//$data_string= urlencode($data_string);
	echo "<div><b>How to call the api</b>:<br>".$site_http."api/thumbnail_edit.php?".$data_string."</div><br><b>Response</b>:<br>";
  $ch = curl_init($site_http.'api/thumbnail_edit.php');   
}
/*************************/
if(isset($_REQUEST['mode'])&&$_REQUEST['mode']!="")
{
  // Configuring curl options

  $options = array(
  CURLOPT_POST => 1,
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS => $data_string
); 

// Setting curl options
curl_setopt_array( $ch, $options ); 
//curl_setopt($ch, CURLOPT_POST, 1);                                                                
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);          
 
$result = curl_exec($ch);
curl_close($ch);
echo $result;

}
else
{
  echo "<ul>";
  echo "<li><a href='http://www.mathinstitutes.org/math-videos/metadata/v1/asset/11'>Get an asset [ID=11]</a></li>";
	 echo "<li><a href='?mode=get'>Get assets for your catalog </a></li>";
  echo "<li><a href='?mode=insert'>Insert a new asset</a> [This will insert a new asset with auto generated ID]</li>";
  echo "<li><a href='?mode=update&assetID=11'>Update an asset [ID=11]</a></li>";
	echo "<li><a href='?mode=delete&assetID=40'>Delete an asset [ID=40]</a></li>";
	echo "<li><a href='?mode=timecode_edit&assetID=11'>Timecode insert/update [ID=11]</a></li>";
	echo "<li><a href='?mode=statistics_edit&assetID=11'>Statistics insert/update [ID=11]</a></li>";
	echo "<li><a href='?mode=thumbnail_edit&assetID=11'>Thumbnail insert/update [ID=11]</a></li>";
  echo "</ul>";
}
//echo  "get image<br>";

db_close($conn);
?>